package com.woniu.his.cms.app.config;

import com.woniu.his.exception.HisException;
import com.woniu.his.result.CommonResultCode;
import com.woniu.his.result.Result;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @version: java version 1.8
 * @Author: Mr tao
 * @description:
 * @date: 2022-12-02 10:49
 */
@RestControllerAdvice
public class GlobalExcetionHandler {

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Result methodArgumentNotValidException(MethodArgumentNotValidException e){
        e.printStackTrace();
        return Result.getInstance().setCode(400).setMsg(e.getBindingResult().getAllErrors().stream().findFirst().get().getDefaultMessage());
    }
    @ExceptionHandler(value = Exception.class)
    public Result globalExcetion(Exception e){
        e.printStackTrace();
        return Result.getInstance().setCode(500).setMsg(e.getMessage());
    };

    @ExceptionHandler(value = HisException.class)
    public Result illegalArgument(HisException e){
        e.printStackTrace();
        return Result.getInstance().setCode(e.getCode()).setMsg(e.getMessage());
    }
}
